package xm.components.ui
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import xm.components.button.SWCButton;
	import xm.components.spinner.SpinnerContainer;
	import xm.core.XMSprite;
	import xm.definition.UIActionButtonType;
	import xm.events.UIEvent;
	import xm.manager.StageManager;
	import xm.manager.UIActionManager;
	
	
	
	
	[Event(name="uiShow", type="xm.events.UIEvent")]
	
	[Event(name="uiHide", type="xm.events.UIEvent")]
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class UIActionSheet extends XMSprite
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * UI 버튼 사이의 갭.
		 */	
		public var gapBewteenUI : Number = 5;
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		protected var confirmBtn : SWCButton;
		protected var cancelBtn : SWCButton;
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _bgLayer : Shape;
		private var uiList : Array = [];
		private var _titleTF : TextField;
		private var _bgTopClip : MovieClip;
		private var _bgClip : MovieClip;
		private var _useCancel : Boolean = true;
		private var _useBackground : Boolean = true;
		private var _useSpinnerBackground : Boolean = false;
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * UIActionSheet Constructor
		 */	
		public function UIActionSheet(useCancel : Boolean = true)
		{
			super();
			
			_useCancel = useCancel;
			
			paddingLeft = 20;
			paddingRight = 20;
			paddingTop = 20;
			paddingBottom = 10;
			
			if( _useCancel ) 
			{
				cancelBtn = new SWCButton( new XM_DEFAULT_SKIN_BUTTON_CANCEL() );
				cancelBtn.label = "취소";
				cancelBtn.textField.textColor = 0x000000;
				cancelBtn.skin.width = cancelBtn.skin.width = this.viewWidth - this.paddingLeft - this.paddingRight;
				cancelBtn.invalidateDisplayList();
				
				uiList.push( cancelBtn );
				addChild( cancelBtn );
				cancelBtn.addEventListener(MouseEvent.CLICK, onCancel );
			}
		}
			
		
		
		public function get useBackground():Boolean
		{
			return _useBackground;
		}

		public function set useBackground(value:Boolean):void
		{
			_useBackground = value;
		}

		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList(enforce);
			
			var len : int , i : int, ui : XMSprite, sumY : Number;
			
			this.viewWidth = StageManager.getInstance().stage.stageWidth;
			
			if( uiList.length > 0 && _useCancel )
			{
				var cancelBtn : SWCButton = uiList[ 0 ] as SWCButton;
				cancelBtn.skin.width = this.viewWidth - this.paddingLeft - this.paddingRight;
			}
			
			sumY = paddingTop;
			
			if( _titleTF )
			{
				_titleTF.x = ( this.viewWidth - _titleTF.width )/2;
				_titleTF.y = sumY;
				sumY += _titleTF.height + gapBewteenUI*2;
			}
			
			
			i = uiList.length;
			
			while( --i>-1 )
			{
				ui = uiList[ i ] as XMSprite;
//				ui.viewWidth = this.viewWidth - this.paddingLeft - this.paddingRight - ui.paddingLeft - ui.paddingRight;;
//				ui.width = ui.viewWidth;
				ui.x = this.paddingLeft + ui.paddingLeft;
				
				
				if( i == 0 && _useCancel )
				{
					sumY += gapBewteenUI*2;
				}
					
				ui.y = sumY + ui.paddingTop;
				
				ui.updateDisplayList( true );
				
				sumY += ( ( ui.viewHeight && ui.viewHeight > 0 ) ? ui.viewHeight : ui.height ) + 
					ui.paddingTop+gapBewteenUI;
				
				if( i == 0 ) sumY += ui.paddingBottom;
			}
			
			this.viewHeight = sumY + paddingBottom;
			
			if( _useBackground )
			{
				if( this.contains( _bgTopClip ) == false ) this.addChild( _bgTopClip );
				if( this.contains( _bgClip ) == false ) this.addChild( _bgClip );
				
				_bgTopClip.width = this.viewWidth;
				_bgClip.width = this.viewWidth;
				_bgClip.height = this.viewHeight - _bgTopClip.height;
			}
			else
			{
				if( this.contains( _bgTopClip ) ) this.removeChild( _bgTopClip );
				if( this.contains( _bgClip ) ) this.removeChild( _bgClip );
			}
			
		}
		
		
		/**
		 * @param ui
		 * @param _useCancel : Boolean (default:true) - 기본적으로 취소버튼 사용여부.
		 */		
		public function addUIAction( ui : XMSprite ):void
		{
			uiList.push( ui );
			addChild( ui );
		}
		
		
		/**
		 * spinner 를 삽입할 경우 사용하는 메서드<br />
		 * cancel 버튼과사용이 false 로 강제되고, 확인버튼이 생긴다. <br />
		 * 패딩도 전부 재설정된다. <br />
		 * gapBewteenUI = 0으로 설정되며 타이틀 사용이 제한된다.<br />
		 * 
		 * <br />
		 * @param spinner : SpinnerContainer 객체.
		 */		
		public function addUISpinnerAction( spinner : SpinnerContainer ):void
		{
			gapBewteenUI = 0;
			_useCancel = false;
			this.padding = [50,0,0,0]
			
//			if( _titleTF )
//			{
//				if( this.contains( _titleTF ) ) this.removeChild( _titleTF );
//				_titleTF = null;
//			}
			
			
			uiList.push( spinner );
			addChild( spinner );
			
			
			confirmBtn = new SWCButton( new XM_DEFAULT_SKIN_CONFIRM_BTN() );
			confirmBtn.addEventListener(MouseEvent.CLICK, onConfirmClick );
			confirmBtn.x = viewWidth-confirmBtn.width-10;
			confirmBtn.y = 15;
			addChild( confirmBtn );
		}
			
		
		
		/**
		 * UIActionSheet 의 children으로 사용되는 기본버튼 4가지 타입 버튼 생성 및 반환 메서드.<br />
		 * <br />
		 * @param type - UIActionType 에 정의된 버튼타입.
		 * @param label - 버튼 레이블
		 * @param labelColor - 버튼 레이블 색상.
		 * @return : SWCButton
		 */		
		public function getCreateDefaultButton( type : String, label : String = "" ):SWCButton
		{
			var swcButton : SWCButton = new SWCButton();
			
			switch( type )
			{
			
			case UIActionButtonType.BUTTON_DEFAULT : 
				swcButton.skin = new XM_DEFAULT_SKIN_BUTTON_DEFAULT();
				if( label != "" ) swcButton.label = label;
				swcButton.textField.textColor = 0x000000;
				break;
			
			case UIActionButtonType.BUTTON_CANCEL : 
				swcButton.skin = new XM_DEFAULT_SKIN_BUTTON_CANCEL();
				if( label != "" ) swcButton.label = label;
				swcButton.textField.textColor = 0x000000;
				break;
			
			case UIActionButtonType.BUTTON_CONFIRM : 
				swcButton.skin = new XM_DEFAULT_SKIN_BUTTON_CONFIRM();
				if( label != "" ) swcButton.label = label;
				swcButton.textField.textColor = 0xFFFFFF;
				break;
			
			case UIActionButtonType.BUTTON_DELETE : 
				swcButton.skin = new XM_DEFAULT_SKIN_BUTTON_DEL();
				if( label != "" ) swcButton.label = label;
				swcButton.textField.textColor = 0xFFFFFF;
				break;
			
			}
			
			swcButton.invalidateDisplayList();
			swcButton.skin.width = this.viewWidth - this.paddingLeft - this.paddingRight;
			return swcButton;
		}
		
		
		/**
		 * 
		 * <br />
		 * <br />
		 * @param index : int
		 * @return XMSprite
		 */		
		public function getUIActionAt( index : int ):XMSprite
		{
			return uiList[ index ] as XMSprite;
		}
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			_bgTopClip = new XM_DEFAULT_SKIN_UI_ACTION_BG_TOP();
			addChild( _bgTopClip );
			
			_bgClip = new XM_DEFAULT_SKIN_UI_ACTION_BG();
			addChild( _bgClip );
			_bgClip.y = _bgTopClip.height;
			
		}
		
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		
		
		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			var i:int, len:int;
			
			gapBewteenUI = NaN;
			
			if( confirmBtn )
			{
				confirmBtn.removeEventListener(MouseEvent.CLICK, onConfirmClick );
				confirmBtn.destroy();
				confirmBtn = null;
			}
			
			if( cancelBtn )
			{
				cancelBtn.removeEventListener(MouseEvent.CLICK, onCancel );
				cancelBtn.destroy();
				cancelBtn = null;
			}
			
			if( _bgLayer )
			{
				if( _bgLayer.parent ) _bgLayer.parent.removeChild( _bgLayer );
				_bgLayer = null;
			}
			
			if( _titleTF )
			{
				if( _titleTF.parent ) _titleTF.parent.removeChild( _titleTF );
				_titleTF = null;
			}
			
			if( _bgTopClip )
			{
				if( _bgTopClip.parent ) _bgTopClip.parent.removeChild( _bgTopClip );
				_bgTopClip = null;
			}
			
			if( _bgClip )
			{
				if( _bgClip.parent ) _bgClip.parent.removeChild( _bgClip );
				_bgClip = null;
			}
			
			
			i=0;
			len=uiList.length;
			while( i<len )
			{
				if( uiList[ i ] is XMSprite )
				{
					( uiList[ i ] as XMSprite ).destroy();
				}
				delete uiList[ i ];
				
				++i;
			}
			uiList = null;
			
			
			
			super.destroy();
			
			
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * @param str 레이블 
		 */		
		public function set title( str : String ):void
		{
			if( _titleTF == null )
			{
				var tf : TextFormat;
				
				tf = new TextFormat( "돋움", 16, 0xFFFFFF, true );
				
				_titleTF = new TextField();
				_titleTF.defaultTextFormat = tf;
				_titleTF.autoSize = TextFieldAutoSize.LEFT;
				_titleTF.selectable = false;
				
				addChild( _titleTF );
			}
			
			_titleTF.text = str;
			_titleTF.width = _titleTF.textWidth + 4;
			_titleTF.height = _titleTF.textHeight + 4;
		}
		
		/**
		 * @return 레이블 텍스트 또는 ""; 
		 */		
		public function get title():String
		{
			return ( _titleTF ) ? _titleTF.text : ""; 
		}
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		private function onCancel( e:MouseEvent ):void
		{
			UIActionManager.getInstance().hideInView();
		}	
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		/**
		 * @param e
		 */		
		protected function onConfirmClick(e:MouseEvent):void
		{
			UIActionManager.getInstance().hideInView();
			
			var uiEvent : UIEvent = new UIEvent( UIEvent.CONFIRM , false, false );
			this.dispatchEvent( uiEvent );
		}	
		
		
		
		
	}
}